package com.blogs.ui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blogs.component.BaseActivity;
import com.blogs.component.BlogDraftAdapter;
import com.blogs.component.GetMoreListView;
import com.blogs.entity.BlogDraftFeed;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.LoadingGif;
import com.blogs.tools.TbBlogDraft;
import com.blogs.ui.CommentQueueActivity.getDataMethod;
import com.cnblogs.R;

public class BlogDraftActivity extends BaseActivity implements OnClickListener,OnItemClickListener{
	private RelativeLayout title_top_bg;
	private TbBlogDraft tb;
	private DBSharedPreferences sp;
	private Button title_include_back;
	private TextView title_include_title;
	private GetMoreListView listView;
	private LoadingGif loadingGif;
	private LinearLayout blog_draft_body;
	private BlogDraftAdapter adapter;
	private ArrayList<BlogDraftFeed> feedList;
	private BlogDraftReceiver blogDraftReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.blog_draft);
		sp= new DBSharedPreferences(this);
		ViewGroup headview=(ViewGroup)findViewById(R.id.main_title_include);
		title_top_bg=(RelativeLayout)headview;
		title_include_title=(TextView)headview.findViewById(R.id.title_include_title);
		title_include_back=(Button)headview.findViewById(R.id.title_include_back);
		title_include_title.setText("草稿箱");
		title_include_back.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(this);
		blog_draft_body=(LinearLayout)findViewById(R.id.blog_draft_body);
		listView=new GetMoreListView(this);
		listView.setOnItemClickListener(this);
		listView.setFootNotMore();
		
		loadingGif=new LoadingGif(this, listView, blog_draft_body,sp);
		loadingGif.setOnReloadListener(new LoadingGif.OnReloadListener() {
			
			public void onClick() {
				// TODO Auto-generated method stub
				requestData();
			}
		});
		this.requestData();
		this.InitSkin();
		
		blogDraftReceiver=new BlogDraftReceiver();//设置广播接收
		IntentFilter skinFilter = new IntentFilter();
        skinFilter.addAction("BlogDraftReceiver");
        registerReceiver(blogDraftReceiver, skinFilter);
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			blog_draft_body.getBackground().setLevel(2);
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			blog_draft_body.getBackground().setLevel(1);
		}
	}
	private void requestData()
	{
		loadingGif.showLoading();
		this.sendTimeLine(getDataMethod.init);
	}
	public void updateRequestData(getDataMethod gdm)
	{
		if(adapter!=null)
		{
			adapter.notifyDataSetChanged();
			if(gdm==getDataMethod.init)
			{
				listView.setAdapter(adapter);
			}
		}
		else
		{
			adapter=new BlogDraftAdapter(this, feedList);
			listView.setAdapter(adapter);
		}

		loadingGif.showData();
	}
	private void sendTimeLine(final getDataMethod gdm)
	{
		tb=new TbBlogDraft(this);
		if(feedList==null)
			feedList=new ArrayList<BlogDraftFeed>();
		if(gdm==getDataMethod.init)
		{
			feedList.clear();
		}
		ArrayList<BlogDraftFeed> feeds=tb.Select();
		for(int i=0;i<feeds.size();i++)
		{
			feedList.add(feeds.get(i));
		}
		updateRequestData(gdm);
		tb.DbClose();
	}
	public void onItemClick(AdapterView<?> parent, View view,final int position,
			long id) {
		// TODO Auto-generated method stub
		if(position>feedList.size())
			return;
		String[] items = { "编辑", "删除", "取消" };
		new AlertDialog.Builder(this).setItems(items, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				switch (which) {
				case 0:
					Intent intent=new Intent(BlogDraftActivity.this,BlogPublicActivity.class);
					intent.putExtra("id", feedList.get(position).id);
					intent.putExtra("txbTitle", feedList.get(position).txbTitle);
					intent.putExtra("EditorBody", feedList.get(position).EditorBody);
					intent.putExtra("name_site_categroy_id", feedList.get(position).name_site_categroy_id);
					intent.putExtra("txbTag", feedList.get(position).txbTag);
					intent.putExtra("txbExcerpt", feedList.get(position).txbExcerpt);
					intent.putExtra("cbHomeCandidate", feedList.get(position).cbHomeCandidate);
					intent.putExtra("cbIsPublishToSiteHome", feedList.get(position).cbIsPublishToSiteHome);
					intent.putExtra("chkDisplayHomePage", feedList.get(position).chkDisplayHomePage);
					startActivity(intent);
					break;
				case 1:
					
					new AlertDialog.Builder(BlogDraftActivity.this)  
					.setTitle("删除草稿") 
					.setMessage("是否删除该草稿？\n删除后将不能恢复") 
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							TbBlogDraft tbd=new TbBlogDraft(BlogDraftActivity.this);
							tbd.Delete(feedList.get(position).id+"");
							tbd.DbClose();
							feedList.remove(feedList.get(position));
							adapter.notifyDataSetChanged();
						}
					}) 
					.setNegativeButton("取消", null) 
					.show();
					break;
				case 2:
					
					break;
				default:

					break;
				}
			}
		}).create().show();
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(blogDraftReceiver);  
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==title_include_back)
		{
			this.finish();
		}
	}
	class BlogDraftReceiver extends BroadcastReceiver
	{
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			requestData();
			listView.setSelection(0);
		}
	}
}
